package org.adl.registry.website;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.adl.registry.website.rim.RimProxy;
import org.adl.registry.types.Registry;

/**
 * Provides methods for supporting the overall operation of the website.
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class PortalUtil
{
    /**
     * 
     * 
     * @return 
     * @param iContext 
     */
    public static String getClientAccessKey(ServletContext iContext)
    {
        String cak = "";
        try
        {
            
            cak = PortalUtil.getProperty(iContext, "portal_cak");
        }
        catch (IOException ex)
        {
            //Do nothing. The CAK will be assumed to be an empty string.
        }
        
        return cak;
    }
    
    /**
     * 
     * 
     * @return 
     * @param iContext 
     * @param iRegistryHandle 
     */
    public static String getRimUrl(ServletContext iContext, String iRegistryHandle)
    {
        String url = "localhost";
        
        try
        {
            if (iRegistryHandle.equalsIgnoreCase(Registry.OPERATIONAL_REGISTRY_HANDLE))
            {
                url = PortalUtil.getProperty(iContext, "or_rim_url");
            }
            else if (iRegistryHandle.equalsIgnoreCase(Registry.PRACTICE_REGISTRY_HANDLE))
            {
                url = PortalUtil.getProperty(iContext, "pr_rim_url");
            }
        }
        catch (IOException ex)
        {
            //Do nothing. The url will be assumed to be localhost;
        }
        
        return url;
    }
    
    /**
     * 
     * 
     * @return 
     * @param iContext 
     */
    public static boolean getIsDebugMode(ServletContext iContext)
    {
        boolean debug = false;
        
        try
        {
            String value = PortalUtil.getProperty(iContext, "debug");
            
            debug = Boolean.parseBoolean(value);
        }
        catch (IOException ex)
        {
            //Do nothing. Debug will be assumed to be false;
        }
        
        return debug;
    }
    
    /**
     * 
     * 
     * @return 
     * @param iContext 
     */
    public static boolean getUseRimResponseTypeParameter(ServletContext iContext)
    {
        boolean use = true;
        
        try
        {
            String value = PortalUtil.getProperty(iContext, "use_rim_response_type");
            
            use = Boolean.parseBoolean(value);
        }
        catch (IOException ex)
        {
            //Do nothing. A RIM response type will be used.
        }
        
        return use;
    }
    
    /**
     * 
     * 
     * @return 
     * @param iContext 
     */
    public static String getSearchResponseXSLPath(ServletContext iContext, String iRegistry)
    {
        String path = "";
        
        try
        {
            if (Registry.PRACTICE_REGISTRY_HANDLE.equalsIgnoreCase(iRegistry))
            {
                path = PortalUtil.getProperty(iContext, "pr_search_response_xsl_path");
            }
            else
            {
                path = PortalUtil.getProperty(iContext, "or_search_response_xsl_path");
            }
        }
        catch (IOException ex)
        {
            //Do nothing. The path will be empty.
        }
        
        return path;
    }
    
    /**
     * 
     * 
     * @return 
     * @param iContext 
     * @param iPropertyName 
     * @throws java.io.FileNotFoundException 
     * @throws java.io.IOException 
     */
    public static String getProperty(ServletContext iContext, String iPropertyName)
    throws FileNotFoundException, IOException
    {
        String path = System.getenv("REGISTRY_WEBSITE_HOME") + iContext.getInitParameter("ConfigPath");
        
        return PortalUtil.getProperty(path, iPropertyName);
    }
    
    /**
     * 
     * @param iConfigFilePath 
     * @param iPropertyName 
     * @throws java.io.FileNotFoundException 
     * @throws java.io.IOException 
     * @return 
     */
    private static String getProperty(String iConfigFilePath, String iPropertyName)
    throws FileNotFoundException, IOException
    {
        String value = "";
        
        Properties properties = new Properties();
        
        String path = iConfigFilePath;
        
        FileInputStream fis = new FileInputStream(iConfigFilePath);
        
        properties.load(fis);
        
        value = properties.getProperty(iPropertyName);
        
        fis.close();
        
        return value;
    }
}